package com.yksj.healthtalk.ui.doctor;

import org.handmark.pulltorefresh.library.PullToRefreshScrollView;
import org.universalimageloader.core.ImageLoader;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.SingleBtnFragmentDialog;
import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonHttpResponseHandler;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.doctor.DoctorCommonServiceActivity.BUTTON;
import com.yksj.healthtalk.utils.FriendUtil;
import com.yksj.healthtalk.utils.JsonParseUtils;
import com.yksj.healthtalk.utils.ToastUtil;

/**
 
 * @author wangtao
 *
 */
public class DoctorInterviewNotPayActivity extends BaseFragmentActivity implements OnClickListener{
	
	private String mData;
	private CustomerInfoEntity mCustomerInfoEntity;
	private ImageView headIv;
	private ImageLoader mImageLoader;
	private TextView name;
	private TextView title_departments;
	private TextView hospital;
	private TextView address;
	private TextView special;
	private TextView money;
	private TextView time;//服务时间
	private TextView content;
	private JSONObject mesgObject;
	private TextView mServcieAddress;
	private String SELECTDATE;
	private TextView mTextview;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.mydoctor_reservation);
		Intent intent = getIntent();
//		intent.putExtra("response", mData);
//		intent.putExtra("mCustomerInfoEntity", mCustomerInfoEntity);
		mData = intent.getStringExtra("response");
		mCustomerInfoEntity = (CustomerInfoEntity) intent.getExtras().get("mCustomerInfoEntity");
		initWidget();
		initData();
	}

	private void initData() {
		
		try {
			JSONObject object = (JSONObject) JSON.parse(mData);
			mesgObject = (JSONObject) object.getJSONArray("TickMesg").get(0);
			money.setText(mesgObject.getString("SERVICE_GOLD")+"元");
			//所享受服务
			mTextview=(TextView) findViewById(R.id.service_content1);
			HttpRestClient.doHttpMyDoctorOrderDetail(SmartFoxClient.getLoginUserId(),mesgObject.getString("ORDER_ID"),mesgObject.getString("PAY_ID"), new JsonHttpResponseHandler(){
			      @Override
			    public void onSuccess(int statusCode,
			    		org.json.JSONObject response) {
			    	super.onSuccess(statusCode, response);
					
			    	mTextview.setText(response.optString("SERVICE_CONTENT"));
					 String timeSpace ;
					if (!TextUtils.isEmpty(timeSpace = FriendUtil.getTimeSpace(mesgObject.getString("SERVICE_START"),mesgObject.getString("SERVICE_END")))) {
						time.setText(timeSpace);
					}else {
						time.setText("");
					}
					content.setText(mesgObject.getString("ADVICE_CONTENT"));
			        }
						});

					} catch (Exception e) {
						finish();
						return;
					}
			
		
		titleTextV.setText("门诊预约服务");
		//头像
		mImageLoader = ImageLoader.getInstance();
		mImageLoader.displayImage(
				mCustomerInfoEntity.getSex(),
				mCustomerInfoEntity.getNormalHeadIcon(), 
				headIv);
		//昵称
		name.setText(mCustomerInfoEntity.getName());
		// 职称和科室
		String office2 = mCustomerInfoEntity.getOfficeName2();
		if (!TextUtils.isEmpty(office2)) {
			office2 = "," + office2;
		}
		if (!TextUtils.isEmpty(mCustomerInfoEntity.getOfficeName1())) {
			title_departments.setText(mCustomerInfoEntity.getDoctorTitle()+ "/" + mCustomerInfoEntity.getOfficeName1() + office2);
		} else {
			title_departments.setText(mCustomerInfoEntity.getDoctorTitle());
		}
		// 医院
		hospital.setText(mCustomerInfoEntity.getHospital());
		// 地址
		if (!TextUtils.isEmpty(mCustomerInfoEntity.getDoctorWorkaddress())) {
			address.setText(mCustomerInfoEntity.getDoctorWorkaddress());
		}else {
			address.setText("");
		}
		// 描述
//		note.setText(mCustomerInfoEntity.getDescription());
		//专长
		if (!TextUtils.isEmpty(mCustomerInfoEntity.getSpecial())) {
			StringBuilder b = new StringBuilder();
			b.append("专长").append("\n").append(mCustomerInfoEntity.getSpecial());
			SpannableStringBuilder builder = new SpannableStringBuilder(b.toString());
			builder.setSpan(new ForegroundColorSpan(Color.BLACK), 0, 2,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			builder.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.tag_note)), 2, b.toString().length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			special.setText(builder);
		} else {
			special.setText("专长");
		}
		SELECTDATE = mesgObject.getString("SERVICE_START");
		if (!TextUtils.isEmpty(SELECTDATE)) {
			SELECTDATE = SELECTDATE.substring(0, 8);
		}
		
	}

	private void initWidget() {
		// TODO Auto-generated method stub
		initTitle();
		((PullToRefreshScrollView) findViewById(R.id.hall)).setLayoutInvisible();
		headIv = (ImageView) findViewById(R.id.info_head);
		name = (TextView)findViewById(R.id.info_name);
		title_departments  = (TextView)findViewById(R.id.title_departments);
		hospital = (TextView)findViewById(R.id.hospital);
		address = (TextView)findViewById(R.id.address);
		special = (TextView)findViewById(R.id.special);
		money = (TextView)findViewById(R.id.money);
		time = (TextView)findViewById(R.id.time);
		content = (TextView)findViewById(R.id.content);
		mServcieAddress = (TextView)findViewById(R.id.servcie_address);
		
		findViewById(R.id.delete).setOnClickListener(this);
		findViewById(R.id.pay).setOnClickListener(this);//退订服务
		titleLeftBtn.setOnClickListener(this);
		
		titleRightBtn2.setText(R.string.sure);
		findViewById(R.id.address_lin).setVisibility(View.VISIBLE);
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.delete:
			HttpRestClient.doHttpDeleteOrder(SmartFoxClient.getLoginUserId(), mCustomerInfoEntity.getId(), mesgObject.getString("ORDER_ID"), new AsyncHander(BUTTON.delete.getId()));
			break;
		case R.id.pay:
			HttpRestClient.doHttpWalletBalanceServlet(SELECTDATE,HttpRestClient.doHttpPayment(null,SmartFoxClient.getLoginUserId(), mCustomerInfoEntity.getId(), mesgObject.getString("SERVICE_TYPE_ID"), mesgObject.getString("SERVICE_ITEM_ID"), mesgObject.getString("SERVICE_TYPE_SUB_ID"),new AsyncHander(v.getId())), new AsyncHander(BUTTON.pay.getId()));
		default:
			break;
		}
	}
	
	
	public void onClickServiceContent(View v){
		Intent intent = new Intent(getApplicationContext(),DoctorServiceActivity.class);
		if (mesgObject != null) {
			intent.putExtra("ORIDERID",mesgObject.getString("ORDER_ID"));
		}else {
			return;
		}
		startActivity(intent);
	}
	
	
	class AsyncHander extends  AsyncHttpResponseHandler{
		
		private int buttonId;
		

		public AsyncHander(int id) {
			this.buttonId = id;
		}

		@Override
		public void onSuccess(int statusCode, String content) {
			try {
				Object object = JSON.parse(content);
				if (object == null && buttonId == BUTTON.delete.getId()) {
					ToastUtil.showShort(getApplicationContext(), "删除订单成功...");
					finish();
				}else if (object instanceof JSONObject) {
					JSONObject object2 = (JSONObject) object;
					String str = JsonParseUtils.filterErrorMessage(content);
					if ( str != null) {//错误信息
						SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(),  str);
					}else if(object2.containsKey("Balance")&& buttonId == BUTTON.pay.getId()) {
						Intent intent = new Intent(getApplicationContext(),SelectPaymentOptionActivity.class);
						intent.putExtra("url", HttpRestClient.doHttpPayment(SELECTDATE,SmartFoxClient.getLoginUserId(), mCustomerInfoEntity.getId(), mesgObject.getString("SERVICE_TYPE_ID"), mesgObject.getString("SERVICE_ITEM_ID"), mesgObject.getString("SERVICE_TYPE_SUB_ID"), new AsyncHander(0)));
						intent.putExtra("money",object2.getString("Balance"));
						intent.putExtra("BindPhone", object2.getString("BindPhone"));
						intent.putExtra("isSetPsw", object2.getString("isSetPsw"));
						intent.putExtra("mCustomerInfoEntity", mCustomerInfoEntity);
						startActivity(intent);
						finish();
					}
				}
			} catch (Exception e) {
			}
			super.onSuccess(statusCode, content);
		}
	}

}
